// pages/goodsDetail/goodsDetail.js
import httpUtil from '../../utils/httpUtil.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: false, //加载
    outline: false, //有无网络
    noData: false,
    state: "1",
    newsType: '1',
    status: 'pay',
    list: []
  },
  chakan: function(e) {
    wx.navigateTo({
      url: '/pages/wuliu/wuliu?data=' + JSON.stringify(e.currentTarget.dataset.id),
    })
  },
  active: function(e) {
    var that = this;
    if (e.currentTarget.dataset.id != that.data.newsType) {
      that.setData({
        newsType: e.currentTarget.dataset.id,
        list: []
      })
      if (that.data.newsType == '1') {
        that.setData({
          state: '1',
        })
      } else if (that.data.newsType == '2') {
        that.setData({
          state: '2',
        })
      }
      that.post()
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function(res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true,
          })
        } else {
          that.setData({
            outline: false,
            list: [],
            currentPage: 1
          })
          that.post()
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        that.setData({
          outline: false,
          list: [],
          currentPage: 1
        })
        that.post()
      }
    })
  },

  //获取订单的列表
  post: function() {
    var that = this;
    that.setData({
      hidden: true,
    })
    var req = {
      token: wx.getStorageSync("token"),
      userid: wx.getStorageSync('userId'),
      state: that.data.state,
    }
    httpUtil.post("/crm/usershoporderlist", req)
      .then(function(res) {
        if (res.data.status == 200) {
          that.setData({
            hidden: false,
          })
          var arr = [];
          for (var i = 0; i < res.data.data.msg.length; i++) {
            arr.push(res.data.data.msg[i])
          }
          that.setData({
            list: arr,
            noData:false
          })
          if (that.data.list.length == 0) {
            that.setData({
              noData: true
            })
          }
        }
      }).catch(function(res) {
        console.log(res);
      });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.currentPage < this.data.paginationInfo.totalPage) {
      this.setData({
        currentPage: ++this.data.currentPage,
      })
    } else {
      this.setData({
        bottom: true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})